package com.bootdo.front.controller;


import com.bootdo.common.annotation.Log;
import com.bootdo.common.constant.RoleConstant;
import com.bootdo.common.utils.*;
import com.bootdo.company.service.CompanyService;
import com.bootdo.front.vo.LoginResultVo;
import com.bootdo.front.vo.LoginVo;
import com.bootdo.front.vo.Result;
import com.bootdo.system.dao.UserRoleDao;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.domain.UserRoleDO;
import com.bootdo.system.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/user")
@RestController
public class UserFrontController {
    @Autowired
    UserService userService;

    @Autowired
    UserRoleDao userRoleDao;

    @Log("用户注册")
    @PostMapping("/register")
    @ResponseBody
    Result register(@RequestBody UserDO user) {
        String userName= user.getUsername().trim();
        int count = userService.queryExistByUserName(userName);
        if(count>0){
            return Result.error("此账号已被注册,请尝试换一个账号注册！");
        }
        user.setStatus(1);
        user.setUsername(userName);
        if ("users".equals(user.getIdentify())) {
            user.setRoleType(RoleConstant.APPLICANT_ROLE);
        } else if ("companys".equals(user.getIdentify())) {
            user.setRoleType(RoleConstant.COMPANY_ROLE);
        }
        user.setPassword(MD5Utils.encrypt(user.getUsername(), user.getPassword()));
        if (userService.save(user) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    @Log("用户注册")
    @PostMapping("/registerpc")
    @ResponseBody
    Result registerpc(UserDO user) {
        String  userName= user.getUsername().trim();
        int count = userService.queryExistByUserName(userName);
        if(count>0){
            return Result.error("此账号已被注册,请尝试换一个账号注册！");
        }
        user.setStatus(1);
        user.setUsername(userName);
        user.setRoleType(RoleConstant.COMPANY_ROLE);
        user.setPassword(MD5Utils.encrypt(user.getUsername(), user.getPassword()));
        if (userService.save(user) > 0) {
            UserRoleDO userRoleDO = new UserRoleDO();
            userRoleDO.setUserId(user.getUserId());
            userRoleDO.setRoleId(RoleConstant.COMPANY_ROLE);
            userRoleDao.save(userRoleDO);
            return Result.ok();
        }
        return Result.error();
    }

    @Log("登录")
    @PostMapping("/login")
    @ResponseBody
    Result ajaxLogin(@RequestBody LoginVo loginModel, HttpServletRequest request) {
        String password = MD5Utils.encrypt(loginModel.getUsername(), loginModel.getPassword());
        UsernamePasswordToken token = new UsernamePasswordToken(loginModel.getUsername(), password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            UserDO user = (UserDO) subject.getPrincipal();
            if (Long.compare(user.getRoleType(), RoleConstant.APPLICANT_ROLE) != 0) {
                ShiroUtils.logout();
                return Result.error("无权限登录");
            }

            LoginResultVo loginResult = new LoginResultVo();
            loginResult.setId(user.getUserId());
            loginResult.setName(user.getName());
            loginResult.setUserName(loginModel.getUsername());
            return Result.ok(loginResult);
        } catch (AuthenticationException e) {
            return Result.error("用户或密码错误");
        }
    }

    @GetMapping("/logout")
    String logout() {
        ShiroUtils.logout();
        return "redirect:/login";
    }


}
